package com.example.demo.controller.admin;



import com.example.demo.dao.Category;
import com.example.demo.dao.CategoryExample;
import com.example.demo.dao.Goods;
import com.example.demo.dao.ImagePath;
import com.example.demo.service.CateService;
import com.example.demo.service.GoodsService;
import com.example.demo.util.ImageUtil;
import com.example.demo.util.Msg;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    CateService cateService;

    //展示商品列表
    @RequestMapping("/searchGoods")
    public String goodsPage(Model model){
        List<Category> categories = cateService.selectByExample(new CategoryExample());
        model.addAttribute("categories",categories);
        return "goods-list";
    }

    @GetMapping("/api/searchGoods")
    @ResponseBody
    public Msg searchGoodsResponse(@RequestParam("pageNum")Integer pageNum,
                                   @RequestParam("pageSize")Integer pageSize, @RequestParam("keyword")String keyword){
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goods = goodsService.listGoods(keyword);
        Msg msg = new Msg();
        return msg.addGoods(((Page)goods).getTotal(),goods);
    }

    //删除选中的单行商品信息
    @DeleteMapping("/delete")
    @ResponseBody
    public Msg deleteGoods(@RequestParam("id")Integer id){
        goodsService.deleteGoodsByPrimaryKey(id);
        return Msg.success("商品信息已删除");
    }

    //删除选中的多行商品信息
    @RequestMapping("/multiDel")
    @ResponseBody
    public Msg multiDelGoods(@RequestParam("ids")String ids){
        goodsService.multiDelGoods(ids);
        return Msg.success("商品信息已删除");
    }

    //获取选中的商品信息
    @GetMapping("/get")
    @ResponseBody
    public Goods getGoods(@RequestParam("id")Integer id){
        return goodsService.findGoodsByPrimaryKey(id);
    }

    //保存修改后的商品信息
    @PostMapping("/edit")
    @ResponseBody
    public Msg updateGoods(Goods goods){
        goodsService.updateGoods(goods);
        return Msg.success("商品信息更新成功");
    }

    //增加商品
    @RequestMapping("/add")
    public String add(@ModelAttribute("successMsg")String msg,Model model){
        //若添加商品成功，则接收/addSuccess传过来的消息并传给add-list页面
        if(!msg.equals("")){
            model.addAttribute("msg",msg);
        }
        //将商品分类传给页面
        List<Category> categories = cateService.selectByExample(new CategoryExample());
        model.addAttribute("categories",categories);
        return "add-goods";
    }
    @RequestMapping("/addGoodsSuccess")
    public String addGoods(Goods goods, @RequestParam MultipartFile[] fileToUpload, RedirectAttributes redirectAttributes){
        goods.setUptime(new Date());
        //goods.setActivityid(1);
        goodsService.addGoods(goods);
        for (MultipartFile multipartFile : fileToUpload){
            /**
             * 对文件名进行操作防止文件重名
             */
            //获取原始文件名，并加工处理得到新的文件名
            String fileName = goods.getGoodsname()+multipartFile.getOriginalFilename();
            if(multipartFile != null){
                //进一步处理文件名，并将文件保存到本地目录中
                String ImagePath = ImageUtil.imagePath(multipartFile,fileName);
                System.out.println("最后存入数据库的图片名字为："+ImagePath);
                //把图片路径存入到数据库中
                goodsService.addImagePath(new ImagePath(null,goods.getGoodsid(),ImagePath));
            }
        }
        redirectAttributes.addFlashAttribute("successMsg","商品添加成功！");
        return "redirect:/goods/searchGoods";
    }

}
